package com.fancy.learncenter.ui.activity;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.FFlibaryAppConfigBean;
import com.fancy.learncenter.bean.FFlibraryGoldLectureListBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.common.LoginUserSharePrefernce;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.activity.base.MallCommonH5Activity;
import com.fancy.learncenter.ui.adapter.FragmentShowVideoAdapter;
import com.fancy.learncenter.ui.adapter.GoldLectureListAdapter;
import com.fancy.learncenter.ui.view.CustomFootView;
import com.fancy.learncenter.ui.view.CustomHeaderView;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.ProgressUtil;
import com.fancy.learncenter.utils.Utils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;
import rx.Subscriber;

public class GoldLectureListActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.right)
    TextView right;
    @Bind(R.id.right_img)
    ImageView rightImg;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.gold_lecture_list_recycle)
    RecyclerView goldLectureListRecycle;
    @Bind(R.id.xRefreshView)
    XRefreshView xRefreshView;
    Dialog dialog;
    List<FFlibraryGoldLectureListBean.ResultBean> resultBeanList;
    GoldLectureListAdapter adapter;
    int pageIndex = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gold_lectuer_list, Status.STATUS, "");
        ButterKnife.bind(this);
        initView();
        initXRefreshView();
        initData();
        getAppConfig();
    }

    private void getAppConfig(){

        String currentTime = String.valueOf(Utils.getCurrentTimeMillis());
        HashMap<String,String> hashMap = new HashMap<>();
        hashMap.put("time", currentTime);
        hashMap.put("AppNo","1001");

        HttpMehtod.getInstance().getAppConfig(hashMap, new Subscriber<Response<ResponseBody>>() {

            @Override
            public void onStart() {
                super.onStart();
                if (dialog != null && !dialog.isShowing()){
                    dialog.show();
                }
            }

            @Override
            public void onCompleted() {

                dialog.dismiss();
            }

            @Override
            public void onError(Throwable e) {

                dialog.dismiss();
            }

            @Override
            public void onNext(Response<ResponseBody> o) {

                String result = NetUtil.getResResult(o);
                Gson gson = new Gson();
                FFlibaryAppConfigBean bean = gson.fromJson(result, FFlibaryAppConfigBean.class);
                LoginUserSharePrefernce.setUrlConfig(bean.getResult().getUrlConfig());

            }
        });
    }


    private void initView() {

        resultBeanList = new ArrayList<>();
        title.setText("金牌老师");
        right.setText("成为主播");
        right.setVisibility(View.GONE);
        dialog = ProgressUtil.creatRequestDialog(this, "正在发送电波");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        goldLectureListRecycle.setLayoutManager(layoutManager);
        adapter = new GoldLectureListAdapter(this, resultBeanList);
        goldLectureListRecycle.setAdapter(adapter);

        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent mallIntent = new Intent();
                mallIntent.putExtra("linkURL", Constant.BECOME_TEACHER_URL);
                mallIntent.setClass(GoldLectureListActivity.this, MallCommonH5Activity.class);
                startActivity(mallIntent);
            }
        });
    }


    private void initData(){
        String currentTime = String.valueOf(Utils.getCurrentTimeMillis());
        Map<String, String> valueData = new HashMap<>();
//        currentTime = "1493000454";
        valueData.put("time", currentTime);
        valueData.put("PageIndex", String.valueOf(pageIndex));
        valueData.put("PageSize","10");

        HttpMehtod.getInstance().getGoldLectureList(valueData, new Subscriber<Response<ResponseBody>>() {
            @Override
            public void onCompleted() {

                dialog.dismiss();
            }

            @Override
            public void onStart() {
                super.onStart();
                if (dialog != null && !dialog.isShowing()) {
                    dialog.show();
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Response<ResponseBody> o) {

                String result = NetUtil.getResResult(o);

                Gson gson = new Gson();
                FFlibraryGoldLectureListBean bean = gson.fromJson(result, FFlibraryGoldLectureListBean.class);
                if ("OK".equals(bean.getCode())){

                    if (!TextUtils.isEmpty(result)) {
                        resultBeanList.addAll(bean.getResult());
                        adapter.notifyDataSetChanged();
                    }else {
                        xRefreshView.setLoadComplete(true);
                        xRefreshView.setPullLoadEnable(true);
                    }
                }

                if (pageIndex == 1) {
                    xRefreshView.stopRefresh();
                }else {
                    xRefreshView.stopLoadMore();
                }


            }
        });
    }


    private void initXRefreshView(){

        xRefreshView.setCustomHeaderView(new CustomHeaderView(this));

        xRefreshView.setPullLoadEnable(true);

        adapter.setCustomLoadMoreView(new CustomFootView(this));

        xRefreshView.setPinnedTime(1000);
        xRefreshView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
            @Override
            public void onRefresh() {
                pageIndex = 1;
                initData();
                resultBeanList.clear();
            }

            @Override
            public void onRefresh(boolean isPullDown) {

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                pageIndex++;
                initData();
            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double headerMovePercent, int offsetY) {

            }
        });

    }




}
